package com.ruoyi.web.controller.system;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.audit.domain.Audit;
import com.ruoyi.audit.domain.InventoryAmount;
import com.ruoyi.audit.service.AuditService;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.company.service.ICompanyOtherServer;
import com.ruoyi.framework.web.service.SysPasswordService;
import com.ruoyi.index.service.IndexService;
import com.ruoyi.paramters.service.ParameterService;
import com.ruoyi.product.domain.Product;
import com.ruoyi.purchase.domain.PurchaseOrder;
import com.ruoyi.purchase.service.PurchaseOrderService;
import com.ruoyi.sale.domain.SaleorderOrder;
import com.ruoyi.sale.service.ISaleorderService;
import com.ruoyi.slow.domain.EChartsClass;
import com.ruoyi.slow.domain.Slow;
import com.ruoyi.slow.service.SlowService;
import com.ruoyi.statistics.domain.Statistics;
import com.ruoyi.statistics.service.StatisticsService;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysMenuService;
import com.ruoyi.warehousing.domain.Warehousing;
import com.ruoyi.warehousing.service.WarehousingService;
import com.ruoyi.warning.domain.Warning;
import com.ruoyi.warning.service.WarningService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 首页
 *
 * @author ruoyi
 */
@RestController
public class SysIndexController
{
    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private SysPasswordService passwordService;

    @Autowired
    private PurchaseOrderService purchaseOrderService;

    @Autowired
    private ISaleorderService iSaleorderService;

    @Autowired
    private ICompanyOtherServer iCompanyOtherServer;

    @Autowired
    private WarehousingService warehousingService;

    @Autowired
    private WarningService warningService;

    @Autowired
    private SlowService slowService;

    @Autowired
    private ISaleorderService saleorderService;

    @Autowired
    private StatisticsService statisticsService;

    @Autowired
    private ParameterService parameterService;

    @Autowired
    private AuditService auditService;


    /** 系统基础配置 */
    @Autowired
    private RuoYiConfig ruoyiConfig;

    /**
     * 访问首页，提示语
     */
    @RequestMapping("/")
    public String index()
    {
        return StringUtils.format("欢迎使用{}后台管理框架，当前版本：v{}，请通过前端地址访问。", ruoyiConfig.getName(), ruoyiConfig.getVersion());
    }


    @GetMapping("/getECharts")
    @ResponseBody
    public String getECharts(@RequestParam(value = "key",required = false) String key, @RequestParam(value = "startTime",required = false)  String startTime, @RequestParam(value = "endTime",required = false)  String endTime) {
        List<EChartsClass> list1 = new ArrayList<>();
        List<EChartsClass> list2 = new ArrayList<>();
        EChartsClass eChartsClass = new EChartsClass();
        ArrayList<String> xAxisList = new ArrayList<>();
        ArrayList<Integer> seriesSaleNumList = new ArrayList<>();
        ArrayList<Double> seriesSaleList = new ArrayList<>();
        if (StringUtils.isEmpty(key) && StringUtils.isEmpty(startTime) && StringUtils.isEmpty(endTime) && !key.equals("0")) {
            list1 = slowService.selectEChartsSevenList(1, null, null);
            for (EChartsClass eChartsClass1 : list1) {
                Date date = eChartsClass1.getBeforeDate();
                SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
                String dateString = sdf.format(date);
                xAxisList.add(dateString);
                seriesSaleNumList.add(eChartsClass1.getNum());
                seriesSaleList.add(eChartsClass1.getMoney());
                eChartsClass1.setShowDate(dateString);
                list2.add(eChartsClass1);
            }
        }
        if (StringUtils.isNotEmpty(startTime) && StringUtils.isNotEmpty(endTime)) {
            list1 = slowService.selectEChartsSevenList(0, startTime, endTime);
            for (EChartsClass eChartsClass1 : list1) {
                Date date = eChartsClass1.getBeforeDate();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String dateString = sdf.format(date);
                xAxisList.add(dateString);
                seriesSaleNumList.add(eChartsClass1.getNum());
                seriesSaleList.add(eChartsClass1.getMoney());
                eChartsClass1.setShowDate(dateString);
                list2.add(eChartsClass1);
            }
        }
        if (StringUtils.isNotEmpty(key) && !key.equals("0")) {
            list1 = slowService.selectEChartsSevenList(Integer.parseInt(key), null, null);
            if (key.equals("3")) {
                for (EChartsClass eChartsClass1 : list1) {
                    xAxisList.add(eChartsClass1.getShowDate());
                    seriesSaleNumList.add(eChartsClass1.getNum());
                    seriesSaleList.add(eChartsClass1.getMoney());
                    list2.add(eChartsClass1);
                }
            }
            if (key.equals("2")) {
                for (EChartsClass eChartsClass1 : list1) {
                    Date date = eChartsClass1.getBeforeDate();
                    SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
                    String dateString = sdf.format(date);
                    xAxisList.add(dateString);
                    seriesSaleNumList.add(eChartsClass1.getNum());
                    seriesSaleList.add(eChartsClass1.getMoney());
                    eChartsClass1.setShowDate(dateString);
                    list2.add(eChartsClass1);
                }
            }
            if (key.equals("1")) {
                for (EChartsClass eChartsClass1 : list1) {
                    Date date = eChartsClass1.getBeforeDate();
                    SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
                    String dateString = sdf.format(date);
                    xAxisList.add(dateString);
                    seriesSaleNumList.add(eChartsClass1.getNum());
                    seriesSaleList.add(eChartsClass1.getMoney());
                    eChartsClass1.setShowDate(dateString);
                    list2.add(eChartsClass1);
                }
            }
            if (list1.size() == 0) {
                for (int i = 6; i >= 0; i--) {
                    Date date = DateUtils.addDays(new Date(), -i);
                    SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
                    String formatDate = sdf.format(date);
                    xAxisList.add(formatDate);
                    seriesSaleNumList.add(0);
                    seriesSaleList.add((double) 0);
                    EChartsClass eChartsClass1 = new EChartsClass();
                    eChartsClass1.setShowDate(formatDate);
                    eChartsClass1.setNum(0);
                    eChartsClass1.setMoney((double) 0);
                    list2.add(eChartsClass1);
                }
            }
        }

        JSONObject jsonobject = new JSONObject();
        String data1 = null;
        if (list2.size() > 0) {
            data1 = JSON.toJSONString(list2);
        }
        jsonobject.put("allSaleList", null == data1 ? "" : data1);
        String data2 = null;
        if (xAxisList.size() > 0) {
            data2 = JSON.toJSONString(xAxisList);
        }
        jsonobject.put("xAxisList", null == data2 ? "" : data2);
        String data3 = null;
        if (seriesSaleList.size() > 0) {
            data3 = JSON.toJSONString(seriesSaleList);
        }
        jsonobject.put("seriesSaleList", null == data3 ? "" : data3);
        String data4 = null;
        if (seriesSaleNumList.size() > 0) {
            data4 = JSON.toJSONString(seriesSaleNumList);
        }
        jsonobject.put("seriesSaleNumList", null == data4 ? "" : data4);
        return jsonobject.toString();
    }

    /**
     * 获取首页指标数据 —— 新
     * @return
     */
    @GetMapping("/getMainDataNew")
    @ResponseBody
    public AjaxResult getMainData() {
        Map<String, Object> result = indexService.mainData();
        return AjaxResult.success(result);
    }

    @Autowired
    private IndexService indexService;

    /**
     * 获取首页指标数据 —— 旧
     * @return
     */
    @GetMapping("/getMainData")
    @ResponseBody
    public String getSchoolUserLog() {
        return indexService.get();
    }

}


